package com.yanggu.mapreduce.groupingcomparator;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/7 16:36
 * @description:
 */
public class OrderBeanReducer extends Reducer<OrderBean, NullWritable, OrderBean, NullWritable> {

    /**
     * 这里需要注意的是，如果定义了分组排序对象。
     * 就不使用key的compareTo方法来判断两个对象是否相同
     *
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(OrderBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        //这里是TOPN的简单写法
        int top = 0;

        for (NullWritable value : values) {
            if (top == 3) {
                break;
            }
            // 写出
            context.write(key, NullWritable.get());
            top++;
        }
    }
}
